package com.tzxs.pay.main.deposit.pop;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tzxs.pay.R;
import com.tzxs.pay.main.deposit.TypeNavigator;

/**
 * 订单状态fragment
 */
public class OrderStatusFragment extends Fragment implements View.OnClickListener {
    private static OrderStatusFragment orderStatusFragment;

    private String typeStr;
    TextView settlemented;
    TextView backed;
    TextView canceled;
    TextView noSettlement;
    TextView all;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.pop_order_status, container, false);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setNavigator((TypeNavigator) context);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        settlemented = view.findViewById(R.id.settlemented);
        backed = view.findViewById(R.id.backed);
        canceled = view.findViewById(R.id.canceled);
        noSettlement = view.findViewById(R.id.no_settlement);
        all = view.findViewById(R.id.all);
        all.setSelected(true);

        settlemented.setOnClickListener(this);
        backed.setOnClickListener(this);
        canceled.setOnClickListener(this);
        noSettlement.setOnClickListener(this);
        all.setOnClickListener(this);
    }

    /**
     * 获取单例
     *
     * @return orderStatusFragment
     */
    public static OrderStatusFragment getInstance() {
        if (orderStatusFragment == null) {
            orderStatusFragment = new OrderStatusFragment();
        }
        return orderStatusFragment;
    }

    private TypeNavigator navigator;

    public void setNavigator(TypeNavigator navigator) {
        this.navigator = navigator;
    }

    @Override
    public void onClick(View v) {
        clearChoose();
        switch (v.getId()) {
            //已结算
            case R.id.settlemented:
                typeStr = "已结算";
                settlemented.setSelected(!settlemented.isSelected());

                break;
            //已退回
            case R.id.backed:
                typeStr = "已退回";
                backed.setSelected(!settlemented.isSelected());

                break;
            //已取消
            case R.id.canceled:
                typeStr = "已取消";
                canceled.setSelected(!settlemented.isSelected());

                break;
            //未结算
            case R.id.no_settlement:
                typeStr = "未结算";
                noSettlement.setSelected(!settlemented.isSelected());
                break;
            //不限
            case R.id.all:
                typeStr = "不限";
                all.setSelected(!settlemented.isSelected());


                break;

        }
        //将选中的type 回调给activity
        navigator.getOrderStatuStr(typeStr);
    }

    /**
     * 清除选择状态
     */
    private void clearChoose() {
        settlemented.setSelected(false);
        backed.setSelected(false);
        canceled.setSelected(false);
        noSettlement.setSelected(false);
        all.setSelected(false);
    }


}
